package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.ErpCollectProductCategoryKeyValue;
import com.erp.gather.domain.bo.ErpCollectProductCategoryKeyValueBo;
import com.erp.gather.domain.vo.ErpCollectProductCategoryKeyValueVo;
import com.erp.gather.mapper.ErpCollectProductCategoryKeyValueMapper;
import com.erp.gather.service.IErpCollectProductCategoryKeyValueService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 泪目entryService业务层处理
 *
 * @author erp
 * @date 2023-04-20
 */
@RequiredArgsConstructor
@Service
public class ErpCollectProductCategoryKeyValueServiceImpl implements IErpCollectProductCategoryKeyValueService {

    private final ErpCollectProductCategoryKeyValueMapper baseMapper;

    /**
     * 查询泪目entry
     * @return
     */
    @Override
    public ErpCollectProductCategoryKeyValueVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询泪目entry列表
     */
    @Override
    public TableDataInfo<ErpCollectProductCategoryKeyValueVo> queryPageList(ErpCollectProductCategoryKeyValueBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpCollectProductCategoryKeyValue> lqw = buildQueryWrapper(bo);
        Page<ErpCollectProductCategoryKeyValueVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询泪目entry列表
     */
    @Override
    public List<ErpCollectProductCategoryKeyValueVo> queryList(ErpCollectProductCategoryKeyValueBo bo) {
        LambdaQueryWrapper<ErpCollectProductCategoryKeyValue> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpCollectProductCategoryKeyValue> buildQueryWrapper(ErpCollectProductCategoryKeyValueBo bo) {
        LambdaQueryWrapper<ErpCollectProductCategoryKeyValue> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCategoryId() != null, ErpCollectProductCategoryKeyValue::getCategoryId, bo.getCategoryId());
        lqw.eq(bo.getKeyId() != null, ErpCollectProductCategoryKeyValue::getKeyId, bo.getKeyId());
        lqw.eq(bo.getValueId() != null, ErpCollectProductCategoryKeyValue::getValueId, bo.getValueId());
        return lqw;
    }

    /**
     * 新增泪目entry
     */
    @Override
    public Boolean insertByBo(ErpCollectProductCategoryKeyValueBo bo) {
        ErpCollectProductCategoryKeyValue add = BeanUtil.toBean(bo, ErpCollectProductCategoryKeyValue.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改泪目entry
     */
    @Override
    public Boolean updateByBo(ErpCollectProductCategoryKeyValueBo bo) {
        ErpCollectProductCategoryKeyValue update = BeanUtil.toBean(bo, ErpCollectProductCategoryKeyValue.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpCollectProductCategoryKeyValue entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除泪目entry
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
